//
//  MHEditCircleViewModel.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/4/20.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHSettingCircleViewModel.h"
#import "MHCommonArrowItemViewModel.h"
#import "MHCircleManageViewModel.h"
#import "MHShareCircleViewModel.h"
#import "MHSetCircleInfoViewModel.h"
#import "MHCircleMemberViewModel.h"
#import "MHWebViewModel.h"
@interface MHSettingCircleViewModel()
@property (nonatomic, readwrite, strong) RACCommand *exitCircle;
@end

@implementation MHSettingCircleViewModel


- (instancetype)initWithServices:(id<MHViewModelServices>)services params:(NSDictionary *)params{
    if (self = [super initWithServices:services params:params]) {
        self.circleInfo = params[MHViewModelUtilKey];        
    }
    return self;
}


- (void)initialize{
    [super initialize];
    self.title = @"圈子设置";
    self.footContentViewColor = MH_MAIN_BACKGROUNDCOLOR_2;
    MHCommonGroupViewModel *group0 = [MHCommonGroupViewModel groupViewModel];
     
    BOOL currentUserIsOwner = [self.circleInfo currentUserIsOwner];
    MHCommonArrowItemViewModel *editCell = [MHCommonArrowItemViewModel itemViewModelWithTitle: currentUserIsOwner ? @"编辑圈子资料":@"圈子资料"];
    editCell.destViewModelClass = MHSetCircleInfoViewModel.class;
    editCell.model = self.circleInfo;
    
    MHCommonArrowItemViewModel *circleMemberCell = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"圈子成员"];
    circleMemberCell.subtitle = [NSString stringWithFormat:@"共%d人",self.circleInfo.memberCount];
    
    circleMemberCell.model = self.circleInfo;
    circleMemberCell.destViewModelClass = MHCircleMemberViewModel.class;
    MHCommonArrowItemViewModel *circleManage = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"圈管理"];
    
    
    MHCommonArrowItemViewModel *shareCircle = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"分享圈子"];
    shareCircle.destViewModelClass = MHShareCircleViewModel.class;
    shareCircle.model = self.circleInfo;
    circleManage.destViewModelClass = MHCircleManageViewModel.class;
    circleManage.model = self.circleInfo;
    
    
    MHCommonArrowItemViewModel *circelActive = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"圈子活动"];
    
    circelActive.operation = ^{
        NSString *url = [NSString stringWithFormat:@"%@reward-project.html?circleId=%d&userId=%d&isactive=%@",[MHConfigureManager h5BaseUrl],self.circleInfo.autoId,[MHHTTPService sharedInstance].currentUser.userId,self.circleInfo.isOpenActivity?@"true":@"false"];
        url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
        MHWebViewModel * webViewModel = [[MHWebViewModel alloc] initWithServices:self.services params:@{MHViewModelRequestKey:request}];
        webViewModel.shouldDisableWebViewClose = YES;
        webViewModel.translucentNavigationBar = YES;
        webViewModel.prefersNavigationBarBottomLineHidden = YES;
        [self.services pushViewModel:webViewModel animated:YES];
    };
    
    
//    if ([self.circleInfo currentUserIsOwner] &&  self.circleInfo.payType == MHCirclPayTypeCharge) {
//        self.dataSource = @[group0,group1,group2,group3,group4,group5];
//    }else
//    {
//        self.dataSource = @[group0,group1,group2,group3,group4];
//    }
    
    MHCommonGroupViewModel *group1 = [MHCommonGroupViewModel groupViewModel];
    NSArray *group0Array;
    
    if (self.circleInfo.isOpenMember || self.circleInfo.identType != MHCircleIdentityTypeGuestMember) {
        group0Array = @[editCell,circleMemberCell,circleManage];
    }else
    {
        group0Array =  @[editCell,circleManage];
    }
    
    if ([self.circleInfo currentUserIsOwner] &&  self.circleInfo.payType == MHCirclPayTypeCharge)
    {
    
        group0Array = [group0Array mutableCopy];
       [(NSMutableArray *)group0Array addObject:circelActive];
    }
    
    group0.itemViewModels = group0Array;
    
    group1.itemViewModels = @[shareCircle];
    
    if ([self.circleInfo currentUserIsOwner] && self.circleInfo.payType == MHCirclPayTypeCharge) {
        group1.footer = self.circleInfo.incomeMgs;
        group1.footColor = MHColorFromHexString(@"#F75260");
        group1.footerHeight = 50;
    }
    self.dataSource = @[group0,group1];
    @weakify(self);
    self.exitCircle = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);

        void (^doNext)(MHHTTPResponse *) = ^(MHHTTPResponse  *response){
            if (response.code == MHHTTPResponseCodeSuccess) {
                [self.services popToRootViewModelAnimated:YES];
            }
        };
        return [[self.services.client exitCircleWithCircleId:self.circleInfo.autoId] doNext:doNext];
    }];
                       
    
}

@end
